package com.j3l11234.myy_sign.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.j3l11234.myy_sign.entity.AttendEntity;
import com.j3l11234.myy_sign.util.MyException;

@Service
public class AttendModel {
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<AttendEntity> getUserAttend(Date startDate, Date endDate,int user_id) throws MyException{
		
		Session session = null;
		List<AttendEntity> attendList = null;
		try {
			session = sessionFactory.openSession();	
			String hql="FROM AttendEntity AS attend WHERE attend.user_id=:user_id AND attend.date BETWEEN :startDate AND :endDate";
			Query query=session.createQuery(hql);
			query.setInteger("user_id", user_id);
			query.setDate("startDate", new java.sql.Date(startDate.getTime()));
			query.setDate("endDate", new java.sql.Date(endDate.getTime()));	
			attendList = (List<AttendEntity>)query.list();	
		} catch (Exception e) {
			e.printStackTrace();
			throw new MyException("数据库访问错误");
		}finally{
			if(session != null){
				session.close();
			}	
		}
		return attendList;
	}
	
	public List<Map<String,Object>> getAttend(Date startDate, Date endDate) throws MyException{
		Session session = null;
		List<Map<String,Object>> attendList = null;
		try {
			session = sessionFactory.openSession();	
			String hql=""
					+ "SELECT attend.date, attend.inTime, attend.outTime, user.alias "
					+ "FROM AttendEntity AS attend, UserEntity user "
					+ "WHERE attend.user_id = user.id AND attend.date BETWEEN :startDate AND :endDate";
				
			Query query=session.createQuery(hql);
			query.setDate("startDate", new java.sql.Date(startDate.getTime()));
			query.setDate("endDate", new java.sql.Date(endDate.getTime()));	
			
			@SuppressWarnings("unchecked")
			List<Object []> list = (List<Object []>)query.list();
			attendList = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < list.size(); i++) {
				Object[] itemContent = list.get(i);
				
				HashMap<String, Object> item =  new HashMap<String, Object>();				
				item.put("date", itemContent[0]);
				item.put("inTime", itemContent[1]);
				item.put("outTime", itemContent[2]);
				item.put("alias", itemContent[3]);
				attendList.add(item);
			}	
		} catch (Exception e) {
			e.printStackTrace();
			throw new MyException("数据库访问错误");
		}finally{
			if(session != null){
				session.close();
			}	
		}
		return attendList;
	}
	
	public AttendEntity addAttend(int user_id, int type, Date date, Date time) throws MyException{
		Session session = null;
		AttendEntity attend = null;
		try {
			session = sessionFactory.openSession();	
			String hql="FROM AttendEntity AS attend WHERE attend.user_id=:user_id AND attend.date=:date";
			Query query=session.createQuery(hql);
			query.setInteger("user_id", user_id);
			query.setDate("date", date);
			attend = (AttendEntity)query.uniqueResult();
			//不存在则新建
			if(attend == null){
				attend = new AttendEntity();
				attend.setUser_id(user_id);
				attend.setDate(date);
			}
			
			if(type == 1){
				if(attend.getInTime() == null){
					attend.setInTime(time);
				}
			}else{
				attend.setOutTime(time);
			}
			
			Transaction tran = session.beginTransaction();  	
			session.saveOrUpdate(attend);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new MyException("数据库访问错误");
		}finally{
			if(session != null){
				session.close();
			}	
		}
		
		return attend;
	}
}
